<template>
  <div class="todo-items">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>任务管理</el-breadcrumb-item>
      <el-breadcrumb-item>任务列表</el-breadcrumb-item>
      <!-- <el-breadcrumb-item>活动详情</el-breadcrumb-item> -->
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="criteria.query"
            clearable
            @clear="clearCriteria"
          >
            <el-button slot="append" icon="el-icon-search" v-on:click="getTodoItems"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="openTodoItemDialog">添加任务</el-button>
        </el-col>
      </el-row>
      <el-table :data="TodoItems" border stripe style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="createdAt" label="日期" width="160" :formatter="dateFormatter"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="name" label="摄氏"></el-table-column>
        <el-table-column label="状态" width="80">
          <template slot-scope="scope">
            <!-- {{scope.row}} -->
            <el-switch v-model="scope.row.isComplete" @change="changeState(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="openEditDialog(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteTodoItem(scope.row.id)"
            ></el-button>
            <el-tooltip effect="dark" content="提示文字" placement="top">
              <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- <el-pagination background layout="prev, pager, next" :total="1000"></el-pagination> -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="criteria.pagenum"
        :page-sizes="[30, 50, 100, 400]"
        :page-size="criteria.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="400"
      ></el-pagination>
    </el-card>

    <el-dialog title="任务信息" :visible.sync="todoItemDialog.visible" @click="closeDialog">
      <el-form :model="todoItemInfo" :rules="todoItemRules" ref="todoItemRef">
        <el-form-item label="活动名称" :label-width="todoItemDialog.labelWidth" prop="name">
          <el-input v-model="todoItemInfo.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动区域" :label-width="todoItemDialog.labelWidth" prop="region">
          <el-select v-model="todoItemInfo.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="todoItemDialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="saveTodoItem">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
// import { dayjs } from '../assets/lib/dayjs.min.js'
// import '../assets/lib/dayjs.min.js'
import dayjs from '../assets/lib/dayjs.min.js'

export default {
  data () {
    return {
      users: [],
      TodoItems: [],
      criteria: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
      currentPage: 0,
      todoItemDialog: {
        visible: false,
        labelWidth: '100px'

      },
      todoItemInfo: {
        name: '',
        region: ''

      },
      todoItemRules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' },
        { min: 2, max: 255, message: '长度在 2 到 255 个字符', trigger: 'blur' }
        ],
        region: [{ required: true, message: '请选择活动区域', trigger: 'change' }]

      }
    }
  },
  methods: {
    dateFormatter: function(row, column, cellValue, index) {
      var date = row[column.property]
      if (!date) { return '' };
      return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
    },
    deleteTodoItem (id) {
      this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: todoItem } = await this.$httpClient.delete('TodoItems/' + id)
        console.log(todoItem)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getTodoItems()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    openEditDialog (todoItem) {
      this.todoItemInfo = todoItem
      this.todoItemDialog.visible = true
      console.log(todoItem)
    },
    openTodoItemDialog () {
      this.todoItemDialog.visible = true
    },
    closeDialog () {
      this.$refs.todoItemRef.resetFields()
    },
    // async searchTodoItems () {
    //   const { data: res } = await this.$httpClient.get('TodoItems', { params: this.criteria })
    //   // const res = await this.$httpClient.get('WeatherForecast', this.criteria)
    //   // console.log(res)
    //   this.TodoItems = res
    // },
    clearCriteria () {
      this.getTodoItems()
    },
    saveTodoItem () {
      this.$refs.todoItemRef.validate(async valid => {
        if (!valid) return
        const { data: todoItem } = await this.$httpClient.post('TodoItems', this.todoItemInfo)
        // console.log(todoItem)
        if (todoItem.id > 0 && todoItem.name) {
          this.todoItemDialog.visible = false
          this.$message.success('添加任务成功')
          this.getTodoItems()
        }
      })
    },
    async getTodoItems () {
      const { data: res } = await this.$httpClient.get('TodoItems', { params: this.criteria })
      // const res = await this.$httpClient.get('WeatherForecast', this.criteria)
      // console.log(res)
      this.TodoItems = res
      //   console.log(res)
    },
    handleSizeChange (pageSize) {
      // console.log(pageSize)
      this.criteria.pagesize = pageSize
      this.getTodoItems()
    },
    handleCurrentChange (page) {
      // console.log(page)
      this.criteria.pagenum = page
      this.getTodoItems()
    },
    async changeState (row) {
      console.log(row)
      // const { data: res } = await this.$httpClient.put(`users/${row.id}/state/${row.state}`)
      // {"type":"https://tools.ietf.org/html/rfc7231#section-6.5.4","title":"Not Found","status":404,"traceId":"|bc09fba6-46246f50f674b2ef."}
      row.id = 2
      const { data: res } = await this.$httpClient.put(`TodoItems/${row.id}`,
        { 'Id': 2, 'Name': 'dddd', 'IsComplete': true })
      if (res.meta.status !== 200) {
        row.state = !row.state
        return this.$message.error('ddd')
      }
      this.$message.success('更新成功')
    }
  },
  created () {
    this.getTodoItems()
  }

}
</script>

<style lang="less" scoped>
.todo-items {
  .el-card{
margin-top: 1rem;
  }
}
</style>
